package solitaire.controle;

import javax.swing.JFrame;

import solitaire.commandes.Command;
import solitaire.commandes.QuitGameCommand;
import solitaire.commandes.QuitGameDisplayCommand;
import solitaire.commandes.RestartGameCommand;
import solitaire.commandes.StartGameDisplayCommand;
import solitaire.presentation.GameWindow;

/**
 * Impl&eacute;menation du moteur du jeu.
 * 
 * @author Andr&eacute; Simon, J&eacute;r&eacute;my Brault
 */
public class Game implements GameInterface {

	private CSolitaire solitaire;
	
	private Command startGameDisplayCommand;
	private Command quitGameDisplayCommand;
	
	private Thread gameThread;
	
	@Override
	public void setSolitaire(CSolitaire solitaire) {
		this.solitaire = solitaire;
	}
	
	@Override
	public CSolitaire getSolitaire() {
		return solitaire;
	}
	
	@Override
	public void setStartGameDisplayCommand(Command startGameDisplayCommand) {
		this.startGameDisplayCommand = startGameDisplayCommand;
	}
	
	@Override
	public void setQuitGameDisplayCommand(Command quitGameDisplayCommand) {
		this.quitGameDisplayCommand = quitGameDisplayCommand;
	}
	
	@Override
	public void startGame() {
		setSolitaire(new CSolitaire("", new CUsine()));
		solitaire.initialiser();
		startGameDisplayCommand.execute();	
		gameThread = new Thread(solitaire);
		gameThread.start();
	}
	
	@Override
	public void restartGame() {
		gameThread.stop();
		startGame();
	}
	
	@Override
	public void quitGame() {
		solitaire = null;
		quitGameDisplayCommand.execute();
	}
	
	public static void main(String[] args) {
		GameInterface game = new Game();
		GameWindow gameWindow = new GameWindow();
		
		Command startGameDisplayCommand = new StartGameDisplayCommand(game, gameWindow);
		Command quitGameDisplayCommand = new QuitGameDisplayCommand(gameWindow);
		
		game.setStartGameDisplayCommand(startGameDisplayCommand);
		game.setQuitGameDisplayCommand(quitGameDisplayCommand);
		
		Command restartGameCommand = new RestartGameCommand(game);
		Command quitGameCommand = new QuitGameCommand(game);
		
		gameWindow.setRestartGameCommand(restartGameCommand);
		gameWindow.setQuitGameCommand(quitGameCommand);
		
		gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		gameWindow.setBounds(20, 20, 650, 500);
		gameWindow.setTitle("Solitaire");
		gameWindow.setResizable(false);
		gameWindow.setVisible(true);
		
		game.startGame();
	}
	
}
